import axios from "axios";

let http = axios.create({
    baseURL: "http://shop.bluej.cn/api",
    timeout: 8000,
});

//获取缓存的token值
let token = sessionStorage.getItem('token')
// console.log(token);
export function post (url, data) {
    return new Promise((resolve, reject) => {
        http
            .post(url, data, {
                headers: {
                    "x-token": token,  //传参
                    "Content-Type": "application/json",
                },
            })
            .then(
                (res) => {
                    resolve(res);
                },
                (err) => {
                    console.log("alex");
                    reject(err);
                }
            );
    });
}

// axios 提供了一个能力
// params可以自动帮我们把参数拼接到url上
// {a:1,b:2} 直接传递即可

export const get = (url, params = {}) => {
    return new Promise((resolve, reject) => {
        http.get(url, {
            params,
            headers: { "x-token": token, }
        }).then(
            (response) => {
                resolve(response.data);
            },
            (err) => {
                reject(err);
            }
        );
    });
};

export const del = (url, params = {}) => {
    return new Promise((resolve, reject) => {
        http.delete(url, {
            params,
            headers: { "x-token": token, }
        }).then(
            (response) => {
                resolve(response.data);
            },
            (err) => {
                reject(err);
            }
        );
    });
};

export const put = (url, params = {}) => {
    return new Promise((resolve, reject) => {
        http.put(url, {
            params,
            headers: { "x-token": token, }
        }).then(
            (response) => {
                resolve(response.data);
            },
            (err) => {
                reject(err);
            }
        );
    });
};